package com.younion.dao;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.common.base.BaseDAO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.*;

 
public class CardEntityDAO extends BaseDAO {

	protected static final Log logger = LogFactory.getLog(CardEntityDAO.class);

	 
	public Collection selectAll() throws BaseException {
		return queryForList("t_Card_Entity.selectAll", null);
	}

	 
	public Collection selectByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return null;
		try {
			return queryForList("t_Card_Entity.selectByMap", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public int selectCountByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return 0;
		try {
			return queryCount("t_Card_Entity.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public List selectForPage(Map condition, Paginator paginator)
			throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "t_Card_Entity.selectAll";
			} else {
				sqlStr = "t_Card_Entity.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public CardEntityVO insertByVo(CardEntityVO originalVo)
			throws BaseException {
		if (originalVo == null)
			return null;
		try {
			return (CardEntityVO) this.insert("t_Card_Entity.insertByVo",
					originalVo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public CardEntityVO[] insertVos(CardEntityVO[] originalVo)
			throws BaseException {
		if (originalVo == null)
			return null;
		try {
			for (int i = 0; i < originalVo.length; i++)
				originalVo[i] = insertByVo(originalVo[i]);
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public Collection insertByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return null;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				CardEntityVO data = (CardEntityVO) it.next();
				data = insertByVo(data);
			}
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return false;
		try {
			this.update("t_Card_Entity.updateByMap", paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateVos(Map[] originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			for (int i = 0; i < originalVo.length; i++) {
				if (!updateByMap(originalVo[i]))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				Map data = (Map) it.next();
				if (!updateByMap(data))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteAll() throws BaseException {
		try {
			return this.getSqlMapClientTemplate().delete(
					"t_Card_Entity.deleteAll", null) > 0 ? true : false;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public CardEntityVO selectById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return null;
		try {
			return (CardEntityVO) this.getSqlMapClientTemplate()
					.queryForObject("t_Card_Entity.selectById", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Card_Entity.deleteById",
					paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteByIds(List list) throws BaseException {
		if (CollectionUtils.isEmpty(list))
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Card_Entity.deleteByIds",
					list);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteByMap(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Card_Entity.deleteByMap",
					map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public int saveReturnKey(CardEntityVO cardEntityVO) throws BaseException {
		try {
			return Integer.parseInt(this.getSqlMapClientTemplate().insert(
					"t_Card_Entity.saveReturnKey", cardEntityVO).toString());
		} catch (Exception e) {
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public int updateReturnKey(CardEntityVO cardEntityVO) throws BaseException {
		try {
			return this.getSqlMapClientTemplate().update(
					"t_Card_Entity.updateReturnKey", cardEntityVO);
		} catch (Exception e) {
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public List selectAllByChannel(Map paramMap) throws BaseException {
		String sqlStr = "";
		try {
			sqlStr = "t_Card_Entity.selectAllByChannel";
			return queryForList(sqlStr, paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public List selectCustomerByCard(Map paramMap) throws BaseException {
		String sqlStr = "";
		try {
			sqlStr = "t_Card_Entity.selectCustomerByCard";
			return queryForList(sqlStr, paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public List selectMyFavorites(Map paramMap) throws BaseException {
		String sqlStr = "";
		try {
			sqlStr = "t_Card_Entity.selectMyFavorites";
			return queryForList(sqlStr, paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public boolean updateFavourAmountByMap(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate().update(
					"t_Card_Entity.updateFavourAmountByMap", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" CardEntityDAO updateFavourAmountByMap(Map paramMap) exception");
		}
	}
	
	public boolean updateSendCardAmountByMap(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate().update(
					"t_Card_Entity.updateSendCardAmountByMap", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" CardEntityDAO updateSendCardAmountByMap(Map paramMap) exception");
		}
	}
	
	public CardEntityVO selectByCustomerAndRand(int customerId) throws BaseException {
		try {
			return (CardEntityVO) this.getSqlMapClientTemplate().queryForObject("t_Card_Entity.selectByCustomerAndRand", customerId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public API_CardEntityVO selectApiCardEntityByMap(Map paramMap) throws BaseException {
		try {
			return (API_CardEntityVO) this.getSqlMapClientTemplate().queryForObject("t_Card_Entity.selectApiCardEntityByMap", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
}